package com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.kongzue.dialog.interfaces.OnDialogButtonClickListener;
import com.kongzue.dialog.util.BaseDialog;
import com.kongzue.dialog.v3.MessageDialog;
import com.kongzue.dialog.v3.WaitDialog;
import com.ninetaildemonfox.zdl.socialproject.base.BaseActivity;
import com.ninetaildemonfox.zdl.socialproject.R;
import com.ninetaildemonfox.zdl.socialproject.mvp.contract.MyDateContract;
import com.ninetaildemonfox.zdl.socialproject.mvp.presenter.MyDatePresenter;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.adapter.RadioStationAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author NineTailDemonFox
 * @date 2019/7/26 16:22
 * 功能描述：我的约会
 * 联系方式：1037438704@qq.com
 */
public class MyDateActivity extends BaseActivity implements MyDateContract {
    @BindView(R.id.image_left_finish)
    ImageView imageleftFinish;
    @BindView(R.id.text_center)
    TextView textCenter;
    @BindView(R.id.text_right)
    TextView textRight;
    @BindView(R.id.recycerView)
    RecyclerView recycerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.frame_gone)
    FrameLayout frame_gone;


    private RadioStationAdapter radioStationAdapter;
    private MyDatePresenter presenter;
    private int page = 1;
    private String member_id;

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_my_date;
    }

    @Override
    protected void onInitialization(Bundle bundle) {
        member_id = getIntent().getExtras().getString("member_id");
        presenter = new MyDatePresenter(me, this);
        textCenter.setText("我的约会");
        imageleftFinish.setVisibility(View.VISIBLE);
        recycerView.setLayoutManager(new LinearLayoutManager(this));
        radioStationAdapter = new RadioStationAdapter(this);
        radioStationAdapter.setJubaoInt(1);
        recycerView.setAdapter(radioStationAdapter);
        setEvents();
    }

    @Override
    protected void onResume() {
        super.onResume();
        http();
    }

    private void setEvents() {
        //刷新
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                http();
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                http();
            }
        });
        //点赞点击事件
        radioStationAdapter.setmOnClick(new RadioStationAdapter.ItemOnClick() {
            @Override
            public void onClick(int position, Map<String, String> data, int type) {
                switch (type) {
                    case 1:
                        //点赞
                        presenter.addOrderGood(token, data.get("id"), position);
                        break;
                    case 2:
                        //结束报名
                        MessageDialog.show(me, "是否结束报名！", "", "确认", "取消")
                                .setOnOkButtonClickListener(new OnDialogButtonClickListener() {
                                    @Override
                                    public boolean onClick(BaseDialog baseDialog, View v) {
                                        presenter.endOrder(token, data.get("id"), position);
                                        return false;
                                    }
                                });
                        break;
                    default:
                }
            }
        });


    }

    private void http() {
        presenter.myOrderList(token, page);
    }

    @OnClick({R.id.image_left_finish})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.image_left_finish:
                finish();
                break;
            default:
        }
    }

    @Override
    public void dynamicListAndroid(List<Map<String, String>> data) {
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadMore();
        if (page == 1) {
            if (data == null || data.size() == 0) {
                frame_gone.setVisibility(View.VISIBLE);
                recycerView.setVisibility(View.GONE);
            } else {
                frame_gone.setVisibility(View.GONE);
                recycerView.setVisibility(View.VISIBLE);
            }
            radioStationAdapter.clear();
            radioStationAdapter.addAll(data);
        } else {
            if (data == null || data.size() == 0) {
                return;
            }
            radioStationAdapter.addAll(data);
        }
    }

    @Override
    public void myDateFailed(String msg) {

    }

    @Override
    public void showLoading() {
        WaitDialog.show(me, "数据加载中");
    }

    @Override
    public void addOrderGood(int item, int count) {
        List<Map<String, String>> allData = radioStationAdapter.getAllData();
        if (count == 2) {
            String good_count = allData.get(item).get("good_count");
            allData.get(item).put("good_count", (Integer.valueOf(good_count) + 1) + "");
        } else {

        }
        radioStationAdapter.notifyItemChanged(item);
    }

    public static void start(Context me, String member_id) {
        Bundle bundle = new Bundle();
        Intent intent = new Intent(me, MyDateActivity.class);
        bundle.putString("member_id", member_id);
        intent.putExtras(bundle);
        me.startActivity(intent);
    }
}
